﻿Imports KOR.Helpers
Imports KOR.Helpers.Data
Imports Telerik.Web
Imports Telerik.Web.UI

Public Class CycleCount
    Inherits Telerik.Web.UI.RadAjaxPage

    Public ReadOnly Property ISessionID As Integer
        Get
            Return CInt(ValueOrDefault(Request.QueryString("sessionid"), 0))
        End Get
    End Property

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        If Not IsPostBack Then
            Search()
        End If
    End Sub

    Private Sub Search()

        Dim searchFor As String = ""
        Dim criteria As String = ""

        If txtKeywords.Text.ToString.Length > 0 Then
            searchFor = txtKeywords.Text
        End If

        If ddlBrands.SelectedIndex > 0 Then

        End If

        If chkFrozen.Checked Then

        End If

        If chkProcessed.Checked Then

        End If

        odsStock.SelectParameters("SearchFor").DefaultValue = searchFor
        odsStock.SelectParameters("Criteria").DefaultValue = criteria

        rgProductInventory.DataBind()
    End Sub

    Public Sub txtFrozen_TextChanged(sender As Object, e As EventArgs)
        Try
            Dim txtAdjustment As RadNumericTextBox = DirectCast(sender, RadNumericTextBox)
            Dim row As GridItem = CType(txtAdjustment.NamingContainer, GridItem)

            Dim InventoryAdjustmentID As Integer = CInt(ValueOrDefault(rgProductInventory.MasterTableView.DataKeyValues(row.ItemIndex).Item("InventorySessionID"), 0))
            Dim ProductID As Integer = CInt(ValueOrDefault(rgProductInventory.MasterTableView.DataKeyValues(row.ItemIndex).Item("ProductID"), 0))
            Dim DetailID As Integer = CInt(ValueOrDefault(rgProductInventory.MasterTableView.DataKeyValues(row.ItemIndex).Item("DetailID"), 0))
            Dim Stock As Double = CDbl(ValueOrDefault(CType(row.FindControl("lbStock"), Label).Text, 0))
            Dim Difference As Double?

            If txtAdjustment.Value.HasValue Then
                Difference = Stock - txtAdjustment.Value
            End If

            SaveAdjustmentEntry(DetailID, ProductID, txtAdjustment.Value, Stock)
            rgProductInventory.DataBind()
        Catch ex As Exception
            WebMsgBox.ShowError(ex.Message)
        End Try
        
    End Sub

    Sub SaveAdjustmentEntry(DetailID As Integer, ProductID As Integer, CountedQuantity As Double, StockQty As Double)

        Try
            Dim dict As New Dictionary(Of String, Object)

            dict.Add("ProductID", ProductID)
            dict.Add("OriginalQuantity", StockQty)
            dict.Add("CountedQuantity", CountedQuantity)
            dict.Add("Difference", StockQty - CountedQuantity)
            dict.Add("CountedDate", DateTime.Now)
            dict.Add("IsCounted", True)

            If DetailID > 0 Then
                BL.InventoryAdjustments.UpdateInventoryAdjustmentDetail(dict, DetailID)
            Else
                'dict.Add("InventorySessionID", DetailID)
                BL.InventoryAdjustments.InsertInventoryAdjustmentDetails(dict)
            End If

        Catch ex As Exception
            Throw ex
        End Try

    End Sub

    Private Sub btnSearch_Click(sender As Object, e As EventArgs) Handles btnSearch.Click
        Search()
    End Sub

    Private Sub btnPostAll_Click(sender As Object, e As EventArgs) Handles btnPostAll.Click
        Try
            Dim dict As New Dictionary(Of String, Object)
            dict.Add("IsPosted", True)
            dict.Add("PostedDate", DateTime.Now)

            If BL.InventoryAdjustments.UpdateInventoryAdjustmentDetail(dict, ISessionID) > 0 Then
                Dim retVal As Integer = BL.InventoryAdjustments.UpdateInventoryAfterProcessing(ISessionID)
            End If

        Catch ex As Exception
            WebMsgBox.ShowError(ex.Message)
        End Try
    End Sub

    Private Sub rgProductInventory_ItemCommand(sender As Object, e As GridCommandEventArgs) Handles rgProductInventory.ItemCommand

        If e.CommandName = "Process" Then

        End If

    End Sub

    Private Sub rgProductInventory_ItemDataBound(sender As Object, e As GridItemEventArgs) Handles rgProductInventory.ItemDataBound
        If e.Item.ItemType = GridItemType.AlternatingItem Or e.Item.ItemType = GridItemType.Item Then
            If CBool(ValueOrDefault(DataBinder.Eval(e.Item.DataItem, "IsPosted"), False)) Then
                e.Item.Enabled = False
                'CType(
            Else
                e.Item.Enabled = True
            End If
        End If
    End Sub
End Class